package com.kim.file;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {

	/**
	 * 获取sharedPreferences
	 * 
	 * @param context
	 * @param sharedPreferencesName
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context context,
			String sharedPreferencesName) {
		return context.getSharedPreferences(sharedPreferencesName,
				Context.MODE_PRIVATE);
	}

	/**
	 * 保持String型数据
	 * 
	 * @param key
	 * @param value
	 * @param sharedPreferencesName
	 * @param context
	 */
	public static void setValue(String key, String value,
			String sharedPreferencesName, Context context) {
		getSharedPreferences(context, sharedPreferencesName).edit()
				.putString(key, value).commit();
	}

	/**
	 * 获取String型数据，默认返回null
	 * 
	 * @param key
	 * @param sharedPreferencesName
	 * @param context
	 * @return
	 */
	public static String getValueString(String key,
			String sharedPreferencesName, Context context) {
		return getSharedPreferences(context, sharedPreferencesName).getString(
				key, null);
	}

	/**
	 * 保持int型数据
	 * 
	 * @param key
	 * @param value
	 * @param sharedPreferencesName
	 * @param context
	 */
	public static void setValue(String key, int value,
			String sharedPreferencesName, Context context) {
		getSharedPreferences(context, sharedPreferencesName).edit()
				.putInt(key, value).commit();
	}

	/**
	 * 获取int型数据，默认返回0
	 * 
	 * @param key
	 * @param sharedPreferencesName
	 * @param context
	 * @return
	 */
	public static int getValueInt(String key, String sharedPreferencesName,
			Context context) {
		return getSharedPreferences(context, sharedPreferencesName).getInt(key,
				0);
	}

	/**
	 * 保持boolean型数据
	 * 
	 * @param key
	 * @param value
	 * @param sharedPreferencesName
	 * @param context
	 */
	public static void setValue(String key, boolean value,
			String sharedPreferencesName, Context context) {
		getSharedPreferences(context, sharedPreferencesName).edit()
				.putBoolean(key, value).commit();
	}

	/**
	 * 获取boolean型数据，默认返回false
	 * 
	 * @param key
	 * @param sharedPreferencesName
	 * @param context
	 * @return
	 */
	public static boolean getValueBoolean(String key,
			String sharedPreferencesName, Context context) {
		return getSharedPreferences(context, sharedPreferencesName).getBoolean(
				key, false);
	}

	/**
	 * 保持float型数据
	 * 
	 * @param key
	 * @param value
	 * @param sharedPreferencesName
	 * @param context
	 */
	public static void setValue(String key, float value,
			String sharedPreferencesName, Context context) {
		getSharedPreferences(context, sharedPreferencesName).edit()
				.putFloat(key, value).commit();
	}

	/**
	 * 获取float型数据，默认返回0f
	 * 
	 * @param key
	 * @param sharedPreferencesName
	 * @param context
	 * @return
	 */
	public static float getValueFloat(String key,
			String sharedPreferencesName, Context context) {
		return getSharedPreferences(context, sharedPreferencesName).getFloat(
				key, 0);
	}
	
	/**
	 * 保持long型数据
	 * 
	 * @param key
	 * @param value
	 * @param sharedPreferencesName
	 * @param context
	 */
	public static void setValue(String key, long value,
			String sharedPreferencesName, Context context) {
		getSharedPreferences(context, sharedPreferencesName).edit()
				.putLong(key, value).commit();
	}

	/**
	 * 获取float型数据，默认返回0f
	 * 
	 * @param key
	 * @param sharedPreferencesName
	 * @param context
	 * @return
	 */
	public static long getValueLong(String key,
			String sharedPreferencesName, Context context) {
		return getSharedPreferences(context, sharedPreferencesName).getLong(
				key, 0l);
	}
}
